var express = require('express');
var router = express.Router();
var common = require('../common');
//获取总在线活动人数
router.get('/', function(req, res, next) {
	console.dir(res.cookie);
	var size = common.getRoomCount(global.sockets);
    res.send(common.objectToJson({roomCount:size}));
});
//获取指定用户是否在线
router.get('/isOnline', function(req, res, next) {
  var id=req.query.id
  res.send(common.isOnline(global.sockets,id));
});

router.get('/test', function(req, res, next) {
  res.send(common.objectToJson({test:"hello"}));
});

//获取指定socketID是否存活
router.get('/checkOnline', function(req, res, next) {
  var id = req.query.id;
  var bo = false;
  for(var i in global.connectionList) {
	  if(i == id) {
		bo = true;
		break;  
	  }
  }
  res.send(common.objectToJson({online:bo}));
});
//获取单人天梯模式top10排行榜
router.get('/top10', function(req, res, next) {
	var pool = common.getPool();
	pool.getConnection(function (err, conn) {
		if (err) {
			console.log("POOL ==> " + err);
			return;
		}
		var time = common.getTimeStamp();
		conn.query("select account,point,head,wx_head from answer_user WHERE roleID=? AND status=? and id>179 order by point DESC,last_login DESC,id LIMIT ?", [2,1,10],function(error, results) {
			if(error) {
				console.log(error);
				return;
			}
			var data = {};
			if(results.length > 0) {
				var index = 1;
				var point = 0;
				for(var i = 0;i < results.length;i++) {
					results[i].index = 1;
					if(point > results[i].point) index++;
					if(index >= 10) results[i].index = index;
					else  results[i].index = '0' + index;
					point = results[i].point;
					data[i] = results[i];
				}
			}
			res.send(common.objectToJson(data));
		});
		conn.release();
	});
});
module.exports = router;
